package com.ceres.crops.controller;

import com.ceres.crops.pojo.Index;
import com.ceres.crops.service.IndexService;
import com.ceres.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/index")
@SuppressWarnings("all")
public class IndexController {
    @Autowired
    private IndexService indexService;

    @GetMapping("/findAll")
    public Result findAll(){
        try {
            List<Index> indexList = indexService.findAll();
            return new Result(true,2000,indexList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    @PutMapping("/update")
    public Result update(@RequestBody Index index,Integer id){
        try {
            index.setId(id);
            indexService.update(index);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody Index index){
        try {
            indexService.add(index);
            return new Result(true,2000);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,2001);
        }
    }
}
